﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace CommonSLT
{
    public partial class SearchControl : DevExpress.XtraEditors.XtraUserControl
    {
        #region variants
        public event KeyEventHandler OnSearchContent_KeyDown = null;
        public event EventHandler<EventArgs> OnBtnClear_Click = null;
        public event EventHandler<EventArgs> OnBtnSearch_Click = null;
        public event EventHandler<EventArgs> OnBtnRefresh_Click = null;
        #endregion variants
        //=========================================================================================

        #region control
        public SearchControl()
        {
            InitializeComponent();
            this.txtSearchContent.KeyDown += new KeyEventHandler(txtSearchContent_KeyDown);
            this.txtSearchContent.LostFocus += new EventHandler(txtSearchContent_LostFocus);
            this.btnClear.Click += new EventHandler(btnClear_Click);
            this.btnSearch.Click += new EventHandler(btnSearch_Click);
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
        }
        #endregion control
        //=========================================================================================

        #region options
        public void SetTooltip(string content)
        {
            this.txtSearchContent.Properties.NullValuePrompt = content;
            this.txtSearchContent.ToolTip = content;
        }
        #endregion options
        //=========================================================================================

        #region SETTERS
        public void SetSearchContent(string content)
        {
            this.txtSearchContent.EditValue = content;
        }

        public void SetFocusToSearchContent()
        {
            this.txtSearchContent.Focus();
        }
        #endregion SETTERS
        //=========================================================================================

        #region GETTERS
        public string GetSearchContent()
        {
            string result = this.txtSearchContent.Text.Trim();
            result = result.Replace("\'", "");
            result = result.Replace("%", "");
            return result;
        }
        #endregion GETTERS
        //=========================================================================================

        #region events
        private void txtSearchContent_KeyDown(object sender, KeyEventArgs e)
        {
            if (OnSearchContent_KeyDown != null)
                OnSearchContent_KeyDown(sender, e);
        }

        private void txtSearchContent_LostFocus(object sender, EventArgs e)
        {
            if (this.txtSearchContent.Text == "")
                this.txtSearchContent.EditValue = null;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (OnBtnClear_Click != null)
                OnBtnClear_Click(sender, e);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (OnBtnSearch_Click != null)
                OnBtnSearch_Click(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (OnBtnRefresh_Click != null)
                OnBtnRefresh_Click(sender, e);
        }
        #endregion events
        //=========================================================================================
    }
}
